package cn.luckdou.commons.utils;

import java.util.List;
//Page [records=null, pagesize=7, pagenum=3, totalpage=8, startIndex=14, totalrecords=50, startPage=1, endPage=8, servletUrl=null]
public class Hpage<T extends Object>{

	private List<T> records; // 						    需要 SET 
	private int pagesize = 10;//每页显示的记录条数             		构造时 完成
	private int pagenum;//用户要看的页码即当前页码               	构造时 完成  传参
	private int totalpage;//总页数						   		构造时 完成
	private int startIndex;//每页开始记录的索引                  	构造时 完成
	private int totalrecords;//总记录条数                       	构造时 完成  传参
	
	//显示的页码
	private int startPage;//						    		构造时 完成
	private int endPage;//						            	构造时 完成
	
	//查询数据时的Servlet的URL
	private String servletUrl;//						需要SET
	
////////////////////////////////////////////////////////////////////	
	/**
	 * 
	 * @param pagesize 每页显示的记录条数
	 * @param pagenum 用户要看的页码即当前页码
	 * @param totalrecords 总记录条数 
	 */
	public Hpage(int pagesize , Integer pagenum,int totalrecords){
		if (pagenum==null || pagenum<0) pagenum=1;
		this.pagesize=pagesize;	
		this.pagenum = pagenum;
		this.totalrecords = totalrecords;		
		begin(5);
	}
	/**
	 * 
	 * @param pagenum 用户要看的页码即当前页码
	 * @param totalrecords 总记录条数 
	 */
	public Hpage(Integer pagenum,int totalrecords){
		if (pagenum==null || pagenum<0) pagenum=1;
		this.pagenum = pagenum;
		this.totalrecords = totalrecords;
		begin(5);		
	}
	/**
	 * 
	 * @param n n是显示页数的个数为奇数
	 */
	private void  begin(int n){
		//计算总页数
		totalpage =(totalrecords-1)/pagesize+1;

//		if (pagenum>totalpage) pagenum=totalpage;

		//计算每页开始记录的索引
		startIndex = (pagenum-1)*pagesize;


		//显示的页码
		if(totalpage<=n){
			startPage = 1;
			endPage = totalpage;
		}else{
			startPage = pagenum-n/2;
			endPage = pagenum+n/2;
			if(startPage<1){
				startPage = 1;
				endPage = n;
			}
			if(endPage>totalpage){
				endPage = totalpage;
				startPage = totalpage-n+1;
			}
		}
	}
////////////////////////////////////////////////////////////////////	
	public List<T>  getRecords() {
		return records;
	}
	public void setRecords(List<T>  records) {
		this.records = records;
	}
	public int getPagesize() {
		return pagesize;
	}
	public void setPagesize(int pagesize) {
		this.pagesize = pagesize;
	}
	public int getPagenum() {
		return pagenum;
	}
	public void setPagenum(int pagenum) {
		this.pagenum = pagenum;
	}
	public int getTotalpage() {
		return totalpage;
	}
	public void setTotalpage(int totalpage) {
		this.totalpage = totalpage;
	}
	public int getStartIndex() {
		return startIndex;
	}
	public void setStartIndex(int startIndex) {
		this.startIndex = startIndex;
	}
	public int getTotalrecords() {
		return totalrecords;
	}
	public void setTotalrecords(int totalrecords) {
		this.totalrecords = totalrecords;
	}
	public int getStartPage() {
		return startPage;
	}
	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}
	public int getEndPage() {
		return endPage;
	}
	public void setEndPage(int endPage) {
		this.endPage = endPage;
	}
	public String getServletUrl() {
		return servletUrl;
	}
	public void setServletUrl(String servletUrl) {
		this.servletUrl = servletUrl;
	}
	@Override
	public String toString() {
		return "Page [records=" + records + ", pagesize=" + pagesize
				+ ", pagenum=" + pagenum + ", totalpage=" + totalpage
				+ ", startIndex=" + startIndex + ", totalrecords="
				+ totalrecords + ", startPage=" + startPage + ", endPage="
				+ endPage + ", servletUrl=" + servletUrl + "]";
	}
}